/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PagarHipoteca.java
 *
 * Created on 13/05/2011, 01:23:42 AM
 */
package logica;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Jorge Maldonado
 */
public class PagarHipoteca extends javax.swing.JDialog {

    /** Creates new form PagarHipoteca */
    DefaultTableModel mod;
    ArrayList<usuario> jugadores;
    usuario actual;

    public PagarHipoteca(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public PagarHipoteca(ArrayList<usuario> jugadores) {
        this(null, true);
        this.jugadores = jugadores;
        for (int i = 0; i < jugadores.size(); i++) {
            jComboBox1.addItem(jugadores.get(i).getNombre());
        }
        mod = new DefaultTableModel(new Object[]{"Id", "Nombre", "Pago"}, 0);
        lote tmp = null;
        for (int i = 0; i < jugadores.get(0).getPropiedades().size(); i++) {
            if (jugadores.get(0).getPropiedades().get(i).getTipo() == propiedad.TIPO_LOTE&&((lote)actual.getPropiedades().get(i)).isHipotecada()) {
                tmp = (lote) jugadores.get(0).getPropiedades().get(i);
                mod.addRow(new Object[]{tmp.getId(), tmp.getDescripcion(), tmp.getCosto() / 2});
            }

        }
        actual = jugadores.get(0);
        jTable1.setModel(mod);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Seleccione un jugador para pagar hipoteca");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Jugador"));

        jLabel2.setText("Nombre");

        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        jLabel3.setText("Seleccione la propiedad que desea pagar la hipoteca");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 456, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel3))
                        .addContainerGap(205, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE))
        );

        jButton1.setText("Pagar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Salir");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 364, Short.MAX_VALUE)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        // TODO add your handling code here:
        String jugador = jComboBox1.getSelectedItem().toString();
        usuario aux = null;
        for (int i = 0; i < jugadores.size(); i++) {
            aux = jugadores.get(i);
            if (jugadores.get(i).getNombre().equals(jugador)) {
                break;
            }
        }
        actual = aux;
        mod = new DefaultTableModel(new Object[]{"Id", "Nombre", "Pago","Color"}, 0);
        lote tmp = null;
        for (int i = 0; i < aux.getPropiedades().size(); i++) {
            if (aux.getPropiedades().get(i).getTipo() == propiedad.TIPO_LOTE&&((lote)actual.getPropiedades().get(i)).isHipotecada()) {
                tmp = (lote) aux.getPropiedades().get(i);
                mod.addRow(new Object[]{tmp.getId(), tmp.getDescripcion(), tmp.getCosto() / 2,tmp.getColorTexto()});
            }
        }
        jTable1.setModel(mod);
    }//GEN-LAST:event_jComboBox1ItemStateChanged

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        int fila = jTable1.getSelectedRow();
        if (fila != -1) {
            propiedad aux = null;
            for (int i = 0; i < actual.getPropiedades().size(); i++) {
                aux = actual.getPropiedades().get(i);
                if (aux.getId() == Integer.parseInt(jTable1.getValueAt(fila, 0).toString())) {
                    if (actual.getDinero() < aux.getPrecio() / 2) {
                        JOptionPane.showMessageDialog(null, "No se puede pagar la hipoteca porque no hay fondos suficientes", "Error", JOptionPane.ERROR_MESSAGE);
                    } else {
                        ((lote) aux).setHipotecada(false);
                        actual.setDinero(actual.getDinero() - aux.getPrecio() / 2);
                        JOptionPane.showMessageDialog(null, "Hipoteca pagada con éxito", "Error", JOptionPane.INFORMATION_MESSAGE);
                        mod = new DefaultTableModel(new Object[]{"Id", "Nombre", "Pago"}, 0);
                        lote tmp = null;
                        for (int j = 0; j < actual.getPropiedades().size(); j++) {
                            if (actual.getPropiedades().get(i).getTipo() == propiedad.TIPO_LOTE&&((lote)actual.getPropiedades().get(i)).isHipotecada()) {
                                tmp = (lote) actual.getPropiedades().get(j);
                                mod.addRow(new Object[]{tmp.getId(), tmp.getDescripcion(), tmp.getCosto() / 2});
                            }
                        }
                        jTable1.setModel(mod);
                    }
                    return;
                }
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
